package com.umbacos.udm.client.arch;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.umbacos.udm.client.model.UdmStatus;

public class Popup extends ModalDialogBox {

	public final static int POPUP_NOBUTTONS=0;
	public final static int POPUP_OK=1;
	public final static int POPUP_CANCEL=2;
	public final static int POPUP_CLOSE=4;

	final HorizontalPanel buttonPanel = new HorizontalPanel();
	public final VerticalPanel dialogVPanel = new VerticalPanel();
	
	Popup up;
	uPopupHandler eventHandler = null;
	
	public Popup(String title, Widget htContent, int buttons){

		this.up=this;
		setText(title);
		setAnimationEnabled(true);

		dialogVPanel.addStyleName("dialogVPanel");
		setGlassEnabled(true);

		if (htContent!=null)
			dialogVPanel.add(htContent);

		if ((buttons & POPUP_CANCEL)==POPUP_CANCEL) {
			buttonPanel.add(new Button("Cancel", new ClickHandler() {
				public void onClick(ClickEvent event) {
					up=null;
					hide();
					}
				}));
		}
		if ((buttons & POPUP_OK)==POPUP_OK) {
			buttonPanel.add(new Button("Ok", new ClickHandler() {
				public void onClick(ClickEvent event) {
					if (eventHandler != null)
						eventHandler.okClickCallback(up);
					else
						hide();
					up=null;
				}
				}));
		}
		if ((buttons & POPUP_CLOSE)==POPUP_CLOSE) {
			buttonPanel.add(new Button("Close", new ClickHandler() {
				public void onClick(ClickEvent event) {
					up=null;
					hide();
					}
				}));
		}
		
		dialogVPanel.add(buttonPanel);
		dialogVPanel.setCellHorizontalAlignment(buttonPanel, HasHorizontalAlignment.ALIGN_RIGHT);
		dialogVPanel.setSpacing(10);
		dialogVPanel.setWidth(htContent.getOffsetWidth() + "px");
		setWidget(dialogVPanel);
		}

	public void showPopup() {
		center();
		((Button)buttonPanel.getWidget(0)).setFocus(true);
	}
	
	public void addEventHandler(uPopupHandler kk){
		eventHandler=kk;
	}
	
	public void setAction(final HourGlass hourglass, final String cmd, final String file, final String para, final UdmStatus udmStatus) {
		addEventHandler(new uPopupHandler() {				
			@Override
			public void okClickCallback(Popup pp) {
				udmStatus.standardRequest(cmd, file, para);
				pp.hide();
			}
		});
	}

	public interface uPopupHandler {
		public void okClickCallback(Popup pp);
	}
}
